// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyAMRStitchCellAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PyAMRStitchCellAttributes
//
// Purpose:
//   Attributes for Stitch Cell Operator
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a AMRStitchCellAttributes.
//
struct AMRStitchCellAttributesObject
{
    PyObject_HEAD
    AMRStitchCellAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewAMRStitchCellAttributes(int);
std::string
PyAMRStitchCellAttributes_ToString(const AMRStitchCellAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    const char *CreateCellsOfType_names = "DualGridAndStitchCells, DualGrid, StitchCells";
    switch (atts->GetCreateCellsOfType())
    {
      case AMRStitchCellAttributes::DualGridAndStitchCells:
          snprintf(tmpStr, 1000, "%sCreateCellsOfType = %sDualGridAndStitchCells  # %s\n", prefix, prefix, CreateCellsOfType_names);
          str += tmpStr;
          break;
      case AMRStitchCellAttributes::DualGrid:
          snprintf(tmpStr, 1000, "%sCreateCellsOfType = %sDualGrid  # %s\n", prefix, prefix, CreateCellsOfType_names);
          str += tmpStr;
          break;
      case AMRStitchCellAttributes::StitchCells:
          snprintf(tmpStr, 1000, "%sCreateCellsOfType = %sStitchCells  # %s\n", prefix, prefix, CreateCellsOfType_names);
          str += tmpStr;
          break;
      default:
          break;
    }

    return str;
}

static PyObject *
AMRStitchCellAttributes_Notify(PyObject *self, PyObject *args)
{
    AMRStitchCellAttributesObject *obj = (AMRStitchCellAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AMRStitchCellAttributes_SetCreateCellsOfType(PyObject *self, PyObject *args)
{
    AMRStitchCellAttributesObject *obj = (AMRStitchCellAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if ((val == -1 && PyErr_Occurred()) || long(cval) != val)
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }

    if (cval < 0 || cval >= 3)
    {
        std::stringstream ss;
        ss << "An invalid CreateCellsOfType value was given." << std::endl;
        ss << "Valid values are in the range [0,2]." << std::endl;
        ss << "You can also use the following symbolic names:";
        ss << " DualGridAndStitchCells";
        ss << ", DualGrid";
        ss << ", StitchCells";
        return PyErr_Format(PyExc_ValueError, ss.str().c_str());
    }

    Py_XDECREF(packaged_args);

    // Set the CreateCellsOfType in the object.
    obj->data->SetCreateCellsOfType(AMRStitchCellAttributes::CreateType(cval));

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
AMRStitchCellAttributes_GetCreateCellsOfType(PyObject *self, PyObject *args)
{
    AMRStitchCellAttributesObject *obj = (AMRStitchCellAttributesObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetCreateCellsOfType()));
    return retval;
}



PyMethodDef PyAMRStitchCellAttributes_methods[AMRSTITCHCELLATTRIBUTES_NMETH] = {
    {"Notify", AMRStitchCellAttributes_Notify, METH_VARARGS},
    {"SetCreateCellsOfType", AMRStitchCellAttributes_SetCreateCellsOfType, METH_VARARGS},
    {"GetCreateCellsOfType", AMRStitchCellAttributes_GetCreateCellsOfType, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
AMRStitchCellAttributes_dealloc(PyObject *v)
{
   AMRStitchCellAttributesObject *obj = (AMRStitchCellAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *AMRStitchCellAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyAMRStitchCellAttributes_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "CreateCellsOfType") == 0)
        return AMRStitchCellAttributes_GetCreateCellsOfType(self, NULL);
    if(strcmp(name, "DualGridAndStitchCells") == 0)
        return PyInt_FromLong(long(AMRStitchCellAttributes::DualGridAndStitchCells));
    if(strcmp(name, "DualGrid") == 0)
        return PyInt_FromLong(long(AMRStitchCellAttributes::DualGrid));
    if(strcmp(name, "StitchCells") == 0)
        return PyInt_FromLong(long(AMRStitchCellAttributes::StitchCells));



    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyAMRStitchCellAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyAMRStitchCellAttributes_methods[i].ml_name),
                PyString_FromString(PyAMRStitchCellAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyAMRStitchCellAttributes_methods, self, name);
}

int
PyAMRStitchCellAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "CreateCellsOfType") == 0)
        obj = AMRStitchCellAttributes_SetCreateCellsOfType(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
AMRStitchCellAttributes_print(PyObject *v, FILE *fp, int flags)
{
    AMRStitchCellAttributesObject *obj = (AMRStitchCellAttributesObject *)v;
    fprintf(fp, "%s", PyAMRStitchCellAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
AMRStitchCellAttributes_str(PyObject *v)
{
    AMRStitchCellAttributesObject *obj = (AMRStitchCellAttributesObject *)v;
    return PyString_FromString(PyAMRStitchCellAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *AMRStitchCellAttributes_Purpose = "Attributes for Stitch Cell Operator";
#else
static char *AMRStitchCellAttributes_Purpose = "Attributes for Stitch Cell Operator";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(AMRStitchCellAttributesType,         \
                  "AMRStitchCellAttributes",           \
                  AMRStitchCellAttributesObject,       \
                  AMRStitchCellAttributes_dealloc,     \
                  AMRStitchCellAttributes_print,       \
                  PyAMRStitchCellAttributes_getattr,   \
                  PyAMRStitchCellAttributes_setattr,   \
                  AMRStitchCellAttributes_str,         \
                  AMRStitchCellAttributes_Purpose,     \
                  AMRStitchCellAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
AMRStitchCellAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &AMRStitchCellAttributesType
         || Py_TYPE(other) != &AMRStitchCellAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    AMRStitchCellAttributes *a = ((AMRStitchCellAttributesObject *)self)->data;
    AMRStitchCellAttributes *b = ((AMRStitchCellAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static AMRStitchCellAttributes *defaultAtts = 0;
static AMRStitchCellAttributes *currentAtts = 0;

static PyObject *
NewAMRStitchCellAttributes(int useCurrent)
{
    AMRStitchCellAttributesObject *newObject;
    newObject = PyObject_NEW(AMRStitchCellAttributesObject, &AMRStitchCellAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new AMRStitchCellAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new AMRStitchCellAttributes(*defaultAtts);
    else
        newObject->data = new AMRStitchCellAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapAMRStitchCellAttributes(const AMRStitchCellAttributes *attr)
{
    AMRStitchCellAttributesObject *newObject;
    newObject = PyObject_NEW(AMRStitchCellAttributesObject, &AMRStitchCellAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (AMRStitchCellAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
AMRStitchCellAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewAMRStitchCellAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef AMRStitchCellAttributesMethods[] = {
    {"AMRStitchCellAttributes", AMRStitchCellAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *AMRStitchCellAttributesObserver = 0;

std::string
PyAMRStitchCellAttributes_GetLogString()
{
    std::string s("AMRStitchCellAtts = AMRStitchCellAttributes()\n");
    if(currentAtts != 0)
        s += PyAMRStitchCellAttributes_ToString(currentAtts, "AMRStitchCellAtts.", true);
    return s;
}

static void
PyAMRStitchCellAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("AMRStitchCellAtts = AMRStitchCellAttributes()\n");
        s += PyAMRStitchCellAttributes_ToString(currentAtts, "AMRStitchCellAtts.", true);
        cb(s);
    }
}

void
PyAMRStitchCellAttributes_StartUp(AMRStitchCellAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyAMRStitchCellAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(AMRStitchCellAttributesObserver == 0)
    {
        AMRStitchCellAttributesObserver = new ObserverToCallback(subj,
            PyAMRStitchCellAttributes_CallLogRoutine, (void *)data);
    }

}

void
PyAMRStitchCellAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete AMRStitchCellAttributesObserver;
    AMRStitchCellAttributesObserver = 0;
}

PyMethodDef *
PyAMRStitchCellAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return AMRStitchCellAttributesMethods;
}

bool
PyAMRStitchCellAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &AMRStitchCellAttributesType);
}

AMRStitchCellAttributes *
PyAMRStitchCellAttributes_FromPyObject(PyObject *obj)
{
    AMRStitchCellAttributesObject *obj2 = (AMRStitchCellAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PyAMRStitchCellAttributes_New()
{
    return NewAMRStitchCellAttributes(0);
}

PyObject *
PyAMRStitchCellAttributes_Wrap(const AMRStitchCellAttributes *attr)
{
    return WrapAMRStitchCellAttributes(attr);
}

void
PyAMRStitchCellAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    AMRStitchCellAttributesObject *obj2 = (AMRStitchCellAttributesObject *)obj;
    obj2->parent = parent;
}

void
PyAMRStitchCellAttributes_SetDefaults(const AMRStitchCellAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new AMRStitchCellAttributes(*atts);
}

